package com.bed.lovewithpatientbed.service.impl;

import com.bed.lovewithpatientbed.dao.BedPutDao;
import com.bed.lovewithpatientbed.dao.HospitalDao;
import com.bed.lovewithpatientbed.dao.OrderDao;
import com.bed.lovewithpatientbed.pojo.*;
import com.bed.lovewithpatientbed.service.StatisticsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author Tyl
 * @version 1.0.0
 * @ClassName Statis ticsImpl.java
 * @Description 统计服务接口的实现类
 * @createTime 2020年10月13日 11:11:00
 */
@Service
@Transactional
public class StatisticsImpl implements StatisticsService {
    @Resource
    HospitalDao hospitalDao;
    @Resource
    OrderDao orderDao;
    @Resource
    BedPutDao bedPutDao;
    @Override
    /*按照医院查询总租金*/
    public BigDecimal selectRentByHospitalId(Integer hospitalId) {
        BigDecimal rentSum = orderDao.selectRentByHospitalId(hospitalId);
        /*修改医院的租金*/
        HospitalExample example = new HospitalExample();
        HospitalExample.Criteria criteria = example.createCriteria();
        criteria.andHospitalIdEqualTo(hospitalId);
        criteria.andHospitalRentEqualTo(rentSum);
        Hospital hospital = hospitalDao.selectByPrimaryKey(hospitalId);
        hospitalDao.updateByExample(hospital,example);
        return rentSum;
    }
    /*按照医院和科室查询租用率*/
    @Override
    public BigDecimal
    selectOccupancyRateByHospitalIdAndDeptId(Integer hospitalId,Integer deptId)
            throws ParseException {
        OrderExample  example = new OrderExample();
        OrderExample.Criteria criteria = example.createCriteria();
        /*设置时间格式*/
        DateFormat dateFormat = new SimpleDateFormat("yy-MM-dd,HH-mm-ss");
        /*当前日期加入查询条件*/
        criteria.andOrderStartTimeLessThanOrEqualTo(new Date());
        /*15天前的日期*/
        String time1 = dateFormat.format(new Date().getTime()-15*24*60*60*1000);
        /*
         * 转换成日期格式加入查询条件
         * */
        Date time2 = dateFormat.parse(time1);
        criteria.andOrderStartTimeGreaterThanOrEqualTo(time2);
        /*
         * 按照医院和科室查询出所有15天内的订单
         * */
        criteria.andHospitalIdEqualTo(hospitalId);
        if(deptId!=null&&!"".equals(deptId)){
            criteria.andDeptIdEqualTo(deptId);
        }
        List<Order> orders = orderDao.selectByExample(example);
        /*
         * 集合中order的数量也就是订单数
         * */
        int orderCount = orders.size();
        /*按照医院id查询已投放床位的总数量*/
        int bedPutCount  = bedPutDao.selectBedQunantityByhospitalId(hospitalId);
        /*得到医院的租用率(int强制转为小数需要*1.0）*/
        BigDecimal OccupancyRate = BigDecimal.valueOf((bedPutCount*1.0)/(orderCount*1.0));
        return OccupancyRate;
    }
    @Override
    /*按照地区查询医院的数量*/
    public Integer selectHospitalQuantityByAddressId(List<Integer> addressId) {
        HospitalExample example = new HospitalExample();
        HospitalExample.Criteria criteria = example.createCriteria();
        criteria.andAddressIdIn(addressId);
        Long l = hospitalDao.countByExample(example);
        Integer count = l.intValue();
        return null;
    }
}
